<template>
  <div>
    <h1>{{ id ? "编辑" : "新建" }}英雄</h1>
    <el-form label-width="80px" @submit.native.prevent>
      <el-tabs type="border-card">
        <el-tab-pane label="基础信息">
          <el-form-item label="英雄名称">
            <el-input
              v-model="model.name"
              placeholder="请输入英雄名称"
            ></el-input>
          </el-form-item>

          <el-form-item label="英雄称号">
            <el-input
              v-model="model.title"
              placeholder="请输入英雄称号"
            ></el-input>
          </el-form-item>

          <el-form-item label="英雄图标">
            <el-upload
              class="avatar-uploader"
              :action="$http.defaults.baseURL + '/upload'"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
            >
              <img v-if="model.avatar" :src="model.avatar" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>

          <el-form-item label="banner">
            <el-upload
              class="avatar-uploader"
              :action="$http.defaults.baseURL + '/upload'"
              :show-file-list="false"
              :on-success="
                res => {
                  model.banner = res.url;
                }
              "
            >
              <img
                v-if="model.banner"
                :src="model.banner"
                class=""
                width="300"
                height="200"
              />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>

          <el-form-item label="英雄类别">
            <el-select
              v-model="model.categories"
              placeholder="请选择"
              multiple
              filterable
            >
              <el-option
                v-for="item in categories"
                :key="item._id"
                :label="item.name"
                :value="item._id"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="英雄难度">
            <el-rate
              style="margin-top: 0.8rem"
              :max="9"
              v-model="model.scores.different"
            ></el-rate>
          </el-form-item>

          <el-form-item label="技能">
            <el-rate
              style="margin-top: 0.8rem"
              :max="9"
              v-model="model.scores.skills"
            ></el-rate>
          </el-form-item>

          <el-form-item label="攻击">
            <el-rate
              style="margin-top: 0.8rem"
              :max="9"
              v-model="model.scores.attack"
            ></el-rate>
          </el-form-item>

          <el-form-item label="生存">
            <el-rate
              style="margin-top: 0.8rem"
              :max="9"
              v-model="model.scores.survive"
            ></el-rate>
          </el-form-item>

          <el-form-item label="顺风出装">
            <el-select v-model="model.items1" placeholder="请选择" multiple>
              <el-option
                v-for="item in items"
                :key="item._id"
                :label="item.name"
                :value="item._id"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="逆风出装">
            <el-select v-model="model.items2" placeholder="请选择" multiple>
              <el-option
                v-for="item in items"
                :key="item._id"
                :label="item.name"
                :value="item._id"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="使用技巧">
            <el-input
              type="textarea"
              placeholder="请输入内容"
              v-model="model.usageTips"
            >
            </el-input>
          </el-form-item>

          <el-form-item label="对抗技巧">
            <el-input
              type="textarea"
              placeholder="请输入内容"
              v-model="model.battleTips"
            >
            </el-input>
          </el-form-item>

          <el-form-item label="团战思路">
            <el-input
              type="textarea"
              placeholder="请输入内容"
              v-model="model.teamTips"
            >
            </el-input> </el-form-item
        ></el-tab-pane>
        <el-tab-pane label="技能">
          <el-button type="success" @click="model.skills.push({})">
            <i class="el-icon-plus"></i>添加技能</el-button
          >
          <el-row type="flex" style="flex-wrap: wrap">
            <el-col :md="12" v-for="(item, index) in model.skills" :key="index">
              <el-form-item label="名称">
                <el-input v-model="item.name"></el-input>
              </el-form-item>

              <el-form-item label="图标">
                <el-upload
                  class="avatar-uploader"
                  :action="$http.defaults.baseURL + '/upload'"
                  :show-file-list="false"
                  :on-success="res => $set(item, 'icon', res.url)"
                >
                  <img v-if="item.icon" :src="item.icon" class="avatar" />
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </el-form-item>

              <el-form-item label="冷却值">
                <el-input v-model="item.delay"></el-input>
              </el-form-item>

              <el-form-item label="消耗">
                <el-input v-model="item.cost"></el-input>
              </el-form-item>

              <el-form-item label="描述">
                <el-input type="textarea" v-model="item.description"></el-input>
              </el-form-item>

              <el-form-item label="小提示">
                <el-input type="textarea" v-model="item.tips"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button
                  size="small"
                  type="danger"
                  @click="model.skills.splice(index, 1)"
                >
                  删除技能</el-button
                >
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>

        <el-tab-pane label="最佳搭档" name="partners">
          <el-button type="success" @click="model.partners.push({})">
            <i class="el-icon-plus"></i>添加英雄</el-button
          >
          <el-row type="flex" style="flex-wrap: wrap">
            <el-col
              :md="12"
              v-for="(item, index) in model.partners"
              :key="index"
            >
              <el-form-item label="英雄">
                <el-select filterable v-model="item.hero">
                  <el-option
                    v-for="hero in heros"
                    :key="hero._id"
                    :label="hero.name"
                    :value="hero._id"
                  ></el-option>
                </el-select>
              </el-form-item>

              <el-form-item label="描述">
                <el-input type="textarea" v-model="item.description"></el-input>
              </el-form-item>

              <el-form-item>
                <el-button
                  size="small"
                  type="danger"
                  @click="model.partners.splice(index, 1)"
                >
                  删除英雄</el-button
                >
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>

        <el-tab-pane label="皮肤管理" name="pifu">
          <el-row type="flex" style="flex-wrap: wrap"> </el-row>
        </el-tab-pane>
      </el-tabs>

      <el-form-item>
        <el-button type="primary" @click="save()">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  data() {
    return {
      model: {
        name: "",
        avatar: "",
        title: "",
        banner: "",
        categories: "",
        scores: {
          different: 0,
          skills: 0,
          attack: 0,
          survive: 0
        },
        usageTips: "",
        battleTips: "",
        teamTips: "",
        skills: [],
        partners: []
      },
      categories: [],
      items: [],
      heros: []
    };
  },
  props: {
    id: { type: String }
  },

  methods: {
    fetchhero() {
      this.$http.get("/rest/heros").then(res => {
        this.heros = res.data;
      });
    },
    fetchitems() {
      this.$http.get("/rest/items").then(res => {
        this.items = res.data;
      });
    },
    handleAvatarSuccess(res) {
      console.log(res);
      this.model.avatar = res.url;
      // this.$set(this.model,'icon',res.url)
    },
    save() {
      if (this.id) {
        this.$http.put(`/rest/heros/${this.id}`, this.model).then(() => {
          this.$message({
            message: "修改成功",
            type: "success"
          });
        });
        this.$router.push("/heros/list");
      } else {
        this.$http.post("/rest/heros", this.model).then(() => {
          this.$message({
            message: "保存成功",
            type: "success"
          });
        });
        this.$router.push("/heros/list");
      }
    },

    fetch() {
      this.$http.get(`/rest/heros/${this.id}`).then(res => {
        // this.model=res.data
        this.model = Object.assign({}, this.model, res.data);
      });
    },

    fetchcategories() {
      this.$http.get(`/rest/categories`).then(res => {
        // this.model=res.data
        this.categories = res.data;
      });
    }
  },

  created() {
    this.fetchitems();
    this.fetchcategories();
    this.id && this.fetch();
    this.fetchhero();
  }
};
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 5rem;
  height: 5rem;
  line-height: 5rem;
  text-align: center;
}
.avatar {
  width: 5rem;
  height: 5rem;
  display: block;
}
</style>
